<html>
<head><meta charset="utf-8"><title>Type checking core::mem::size_of · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html">Type checking core::mem::size_of</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223815931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223815931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223815931">(Jan 24 2021 at 14:15)</a>:</h4>
<p><code>core::mem::size_of</code> requires the argument to have <code>Sized</code> bound, which makes sense, but I can't see that bound in the definition of the function:</p>
<div class="codehilite"><pre><span></span><code>#[inline(always)]
#[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]
#[rustc_promotable]
#[rustc_const_stable(feature = &quot;const_size_of&quot;, since = &quot;1.32.0&quot;)]
pub const fn size_of&lt;T&gt;() -&gt; usize {
    intrinsics::size_of::&lt;T&gt;()
}
</code></pre></div>
<p>I have two questions:</p>
<ul>
<li>Why do we not have <code>T: Sized</code> bound here?</li>
<li>How are uses of this function type checked currently?</li>
</ul>
<p>I also tried this:</p>
<div class="codehilite"><pre><span></span><code>fn test&lt;F&gt;(f: F)
where
    F: Fn() -&gt; usize,
{
    println!(&quot;{}&quot;, f());
}

fn main() {
    test(core::mem::size_of::&lt;usize&gt;);
    test(core::mem::size_of::&lt;dyn core::fmt::Debug&gt;);
}
</code></pre></div>
<p>The second <code>test</code> call fails with</p>
<div class="codehilite"><pre><span></span><code>   --&gt; test.rs:10:10
    |
10  |     test(core::mem::size_of::&lt;dyn core::fmt::Debug&gt;);
    |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn&#39;t have a size known at compile-time
    |
</code></pre></div>
<p>I'm a bit surprised by this because the error is not in the type argument of <code>size_of</code> but rather it's the entire <code>size_of</code> function/closure. I don't understand why it's not sized.</p>
<p>Could anyone point me to the relevant code in the compiler on how type checking <code>size_of</code> works?</p>



<a name="223816020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816020">(Jan 24 2021 at 14:17)</a>:</h4>
<p><span class="user-mention" data-user-id="119169">@osa1</span> <code>T: Sized</code> is on by default unless you opt-out with <code>T: ?Sized</code></p>



<a name="223816086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816086">(Jan 24 2021 at 14:18)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span>  Ahh, right.. Thanks.</p>



<a name="223816117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816117">(Jan 24 2021 at 14:20)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> any ideas about the type error in the second <code>test</code> call in my example?</p>



<a name="223816160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816160">(Jan 24 2021 at 14:20)</a>:</h4>
<p><span class="user-mention" data-user-id="119169">@osa1</span> I don't know where it's emitted, but it's for the same reason: <code>test&lt;F&gt;</code> has an implicit <code>F: Sized</code> bound</p>



<a name="223816174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816174">(Jan 24 2021 at 14:21)</a>:</h4>
<p>and really the question doesn't make sense in the first place: You <em>can't</em> know the size of a DST, that's the reason it's 'dynamically sized'</p>



<a name="223816180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816180">(Jan 24 2021 at 14:21)</a>:</h4>
<p>do you want to know <code>size_of&lt;&amp;dyn Debug&gt;</code>?</p>



<a name="223816192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816192">(Jan 24 2021 at 14:21)</a>:</h4>
<p>No, I understand that the program should fail. I'm confused by the way it fails. I'd expect the error span to be the <code>dyn core::fmt::Debug</code> part, not the whole <code>test</code> argument.</p>



<a name="223816241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816241">(Jan 24 2021 at 14:22)</a>:</h4>
<p>oh that's probably just bad error reporting <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="223816243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816243">(Jan 24 2021 at 14:22)</a>:</h4>
<p>I'd expect this:</p>
<div class="codehilite"><pre><span></span><code>    |
10  |     test(core::mem::size_of::&lt;dyn core::fmt::Debug&gt;);
    |                               ^^^^^^^^^^^^^^^^^^^^ doesn&#39;t have a size known at compile-time
    |
</code></pre></div>



<a name="223816269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816269">(Jan 24 2021 at 14:23)</a>:</h4>
<p>Worth reporting?</p>



<a name="223816319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816319">(Jan 24 2021 at 14:24)</a>:</h4>
<p>sure, seems reasonable</p>



<a name="223816499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816499">(Jan 24 2021 at 14:29)</a>:</h4>
<p>Hmm, nvm, I think the error message is fine, especially if you read the whole thing:</p>
<div class="codehilite"><pre><span></span><code>error[E0277]: the size for values of type `dyn Debug` cannot be known at compilation time
   --&gt; test.rs:10:10
    |
10  |     test(core::mem::size_of::&lt;dyn core::fmt::Debug&gt;);
    |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn&#39;t have a size known at compile-time
    |
   ::: /home/omer/rust/rust/library/core/src/mem/mod.rs:309:22
    |
309 | pub const fn size_of&lt;T&gt;() -&gt; usize {
    |                      - required by this bound in `std::mem::size_of`
    |
    = help: the trait `Sized` is not implemented for `dyn Debug`
</code></pre></div>
<p>The second message points at the <code>size_of</code>'s type argument.</p>



<a name="223816656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223816656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223816656">(Jan 24 2021 at 14:33)</a>:</h4>
<p><a href="https://rust.godbolt.org/z/zfrjce">https://rust.godbolt.org/z/zfrjce</a> looks like dyn-s lose some info somewhere</p>



<a name="223817254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223817254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223817254">(Jan 24 2021 at 14:48)</a>:</h4>
<p>On a somewhat related note, I wish <code>rustc_on_unimplemented</code> had a userspace alternative <span aria-label="frown" class="emoji emoji-1f641" role="img" title="frown">:frown:</span></p>



<a name="223817263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223817263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223817263">(Jan 24 2021 at 14:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="334260">Dániel Buga</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of/near/223817254">said</a>:</p>
<blockquote>
<p>On a somewhat related note, I wish <code>rustc_on_unimplemented</code> had a userspace alternative :(</p>
</blockquote>
<p><a href="https://jyn514.github.io/2020/09/05/Rust-in-2021.html">me too</a></p>



<a name="223823362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20checking%20core%3A%3Amem%3A%3Asize_of/near/223823362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20checking.20core.3A.3Amem.3A.3Asize_of.html#223823362">(Jan 24 2021 at 16:59)</a>:</h4>
<p>Hmm, this is one of those places where it might be interesting if <code>!Sized</code> were auto-implemented.  Would be nice to say more strongly in that <code>help</code> that not only is it "not [currently] implemented", but that "it explicitly cannot be implemented".</p>
<p>(AKA now that the query isn't just Yes/Maybe but Yes/Maybe/No, the sized query could return No for <code>dyn Trait</code>.)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>